const app = getApp()
const db = wx.cloud.database();
const _ = db.command;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        form: {
            wt1: '',
            wt2: '',
            wt3: '',
            wt4: [],
            wt5: '',
            wt6: '',
            wt7: '',
            wt8: '',
            wt9: '',
            wt10: ''
        },
        date: '',
        kn: [{
                val: "无"
            },{
                val: "吃饭"
            }, {
                val: "洗澡"
            }, {
                val: "上下床"
            }, {
                val: "穿衣"
            }, {
                val: "室内行走"
            }, {
                val: "打扫卫生"
            }, {
                val: "上厕所"
            }, {
                val: "买菜购物"
            }
        ],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {},

    //提交问卷按钮
    submit: function (e) {
        let openid = app.globalData.openid;
        let name = app.globalData.name;
        let type = app.globalData.type;
        let date = this.data.date;
        console.log(e.detail.value)
        this.data.form = e.detail.value
        //判断是否已全部填写
        const {
            wt1,
            wt2,
            wt3,
            wt4,
            wt5,
            wt6,
            wt7,
            wt8,
            wt9,
            wt10
        } = this.data.form
        /*if (wt1 == '' || wt2 == '' || wt3 == '' || wt4.length == 0 || wt5 == '' || wt6 == '' || wt7 == '' || wt8 == '' ||wt9 == '' || wt10== '') {
            wx.showToast({
                title: '请回答完问题',
                icon: 'none'
            })
        } else */
        if (this.data.date == '') {
            wx.showToast({
                title: '请选择日期',
                icon: 'none'
            })
        } else {
            wx.cloud.callFunction({
                name: 'wenjuanget',
                data: {
                    type,
                    openid
                },
                success: res => {
                    console.log('获取问卷openid', res)
                    if (res.result.data.length != 0) {
                        wx.cloud.callFunction({
                            name: 'wenjuanupd',
                            data: {
                                type,
                                openid,
                                name,
                                date,
                                ...this.data.form
                            },
                            success: ress => {
                                console.log('问卷信息更新成功: ', ress)
                            },
                            fail: ress => {
                                console.log('问卷信息更新失败: ', ress)
                            }
                        })
                    } else {
                        wx.cloud.callFunction({
                            name: 'wenjuanadd',
                            data: {
                                type,
                                openid,
                                name,
                                date,
                                ...this.data.form
                            },
                            success: ress => {
                                console.log('添加新问卷信息: ', ress)
                            },
                            fail: ress => {
                                console.log('添加新问卷信息失败: ', ress)
                            }
                        })
                    }
                },
                fail: res => {
                    console.log('获取问卷openid失败', res)
                }

            })



            wx.reLaunch({
                url: '/pages/QRcode/QRcode',
            })
        }
    },
    //选择提交日期
    bindDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            date: e.detail.value
        })
    },

})